﻿<div class="ld-tabs">
    <ul>
        <li class="k-state-active">客户列表</li>
    </ul>
    <div>
        <div class="k-block divToolbar">
            <div style="display: inline-block">
                <select id="ddlSelType">
                    <option value="0">全部</option>
                    <option value="1">顾客</option>
                    <option value="2">供应商</option>
                </select>
            </div>

            <div style="display: inline-block;vertical-align:top">
                <select id="txtCategories" style="width: 200px;" ></select>
            </div>
            <span class="k-textbox k-space-right" style="vertical-align:top">
                <input type="text" placeholder="请输入关键字" id="txtKey" />
                <a href="javascript:FilterCustomers()" class="k-icon k-i-search">&nbsp;</a>
            </span>
            <button class="k-button" onclick="CreateNewCustomer()"><span class="k-icon k-i-plus"></span><span>添加新客户</span></button>
        </div>
        <div id="grid_customer" style="text-align: center"></div>
    </div>
</div>

<script class="init">
    LocatedMenu("成员管理", "客户管理");
    $("#txtCategories").kendoMultiSelect({
        placeholder: "请选择所属分类...",
        dataTextField: "CategoryName",
        dataValueField: "ID",
        dataSource: Whale.dataSource({
            url: "/Members/CustomerCategory/GetCategories"
        }),
        change: function (e) {
            //console.log(this.value().join(','));
            FilterCustomers();
        }
    });
</script>

<script class="grid_customer_columns">
    $("#ddlSelType").kendoDropDownList({
        change: function (e) {
            FilterCustomers();
        }
    });
    var customerFields = [{
        title: "#",
        field: "SortIndex",
        width: 50
    }, {
        title: "拼音",
        field: "Pinyin",
        width: 60
    }, {
        title: "姓名",
        field: "RealName",
        width: 90
    }, {
        title: "手机",
        field: "Mobile",
        width: 110
    }, {
        title: "详情",
        template: "公司：#=Corporation# <br /> 地址：#=Address#",
        width: 230,
        attributes: { style: "text-align:left" }
    }, {
        title: "所属分类",
        template: "#=ShowCategories(Categories)#",
        width: 100
    }, {
        title: "状态",
        template: "#=GeneralStatus(Status)#",
        width: 50
    }, {
        title: "操作",
        template: "#=GetOperations(ID)#",
        attributes: { style: "text-align:left" }
    }];

    function ShowCategories(cats) {
        var html = "";
        $(cats).each(function (i) {
            html += "," + cats[i].CategoryName;
        });
        return html.substring(1);
    }

    function GetOperations(id) {
        var html = "<button class='k-button' onclick=\"EditCustomer(" + id + ")\"><span class='k-icon k-i-pencil'></span><span>修改</span></button>";
        html += "<button class='k-button' onclick=\"DeleteCustomer(" + id + ")\"><span class='k-icon k-i-close'></span><span>删除</span></button>";
        return html;
    }

    function EditCustomer(id) {
        if (wndGeneralEdit != null && typeof wndGeneralEdit != "undefined") {
            wndGeneralEdit.setOptions({ title: "修改客户信息" });
            wndGeneralEdit.refresh({ url: "/Members/Customer/Edit/" + id });
            wndGeneralEdit.open();
        }
    }

    function DeleteCustomer(id) {
        if (confirm("真的要删除这个顾客吗?")) {
            Whale.Remote.Operation({
                url: "/Members/Customer/Delete",
                parameters: { customerID: id },
                success: function (result) {
                    if ($("#grid_customer").length > 0)
                        $("#grid_customer").data("kendoGrid").dataSource.read();
                },
                error: function (msg) {
                    wndGeneralInformation.content(msg).open();
                }
            });
        }
    }

</script>

<script class="grid_config">
    $(function () {
        $("#grid_customer").kendoGrid({
            columns: customerFields,
            dataSource: Whale.dataSource({
                url: "/Members/Customer/Gets",
                getParameters: function () {
                    return {
                        key: $.trim($("#txtKey").val()),
                        getCategory: true,
                        cType: $("#ddlSelType").data("kendoDropDownList").value(),
                        categories: $("#txtCategories").data("kendoMultiSelect").value().join(',')
                    };
                },
                pageSize: 20

            }),
            pageable: {
                messages: {
                    empty: "暂无数据"
                }
            },
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            }
        });
    });

    function CreateNewCustomer() {
        wndGeneralEdit.setOptions({ title: "添加新的客户" });
        wndGeneralEdit.refresh({ url: "/Members/Customer/Edit" });
        wndGeneralEdit.open();
    }

    function FilterCustomers() {
        $("#grid_customer").data("kendoGrid").dataSource.page(0);
        $("#grid_customer").data("kendoGrid").dataSource.read();
    }
</script>
